//
//  BLFullBarrageViewController.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/6/27.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit
import BarrageRenderer

class BLFullBarrageViewController: UIViewController {
    
    var renderer : BarrageRenderer?
    var timer : NSTimer?
    var index : NSInteger?
    var barrageArray = [String]()
    var roomID = ""
    
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        index = 0
        initBarrageRenderer()
        
        // Do any additional setup after loading the view.
    }
    
    
    /// 读取数据
    func receiveNotification(notification : NSNotification) {
        
        let dataStr = ((notification.object)! as! String)
        
        let model : BLDanmuModel = BLDanmuModel()
        
        
        let typeStr = bl_regexWithPattern(DanmuParameterRegex.DanmuType.rawValue, inputStr: dataStr)[0]
        
        if typeStr == BLCellType.CellChatMessage.rawValue {
            model.type = BLCellType.CellChatMessage
            model.setModelFromStirng(dataStr)
        }
        else if typeStr == BLCellType.CellGiftGivingNotice.rawValue {
            model.type = BLCellType.CellGiftGivingNotice
            model.setModelFromStirng(dataStr)
        }
        else if typeStr == BLCellType.CellUserEnter.rawValue {
            model.type = BLCellType.CellUserEnter
            model.setModelFromStirng(dataStr)
        }
        else if typeStr == BLCellType.CellBanUser.rawValue {
            model.type = BLCellType.CellBanUser
            model.setModelFromStirng(dataStr)
        }
        else if typeStr == BLCellType.CellUpGrade.rawValue {
            model.type = BLCellType.CellUpGrade
            model.setModelFromStirng(dataStr)
        }
        else if typeStr == BLCellType.CellKeeplive.rawValue {
            /// 保持连接
        }
        else{
            print(dataStr)
        }
        
        guard model.text != nil else{
            return
        }
        
        let spriteNumber = renderer?.spritesNumberWithName(nil)
        if spriteNumber! < 50 {
            let descriptor = walkTextSpriteDescriptorWithDirection(BarrageWalkDirection.R2L,textStr: model.text!)
            renderer?.receive(descriptor)
        }
        
        
    }
    
    
    
    
    func initBarrageRenderer() {
        renderer = BarrageRenderer()
        view.addSubview((renderer?.view)!)
        renderer?.canvasMargin = UIEdgeInsetsMake(20, 0, 0, 0)
        renderer?.masked = false
        view.sendSubviewToBack((renderer?.view)!)
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        BLDanmuServerTool.shareInstance.startServer(roomID)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(receiveNotification(_:)), name: kReceiveMessageNotification, object: nil)
        renderer?.start()
    }
    
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        renderer?.stop()
        timer?.invalidate()
        BLDanmuServerTool.shareInstance.cutServer()
        NSNotificationCenter.defaultCenter().removeObserver(kReceiveMessageNotification)
    }
    
    
    /// 生成精灵描述 - 过场文字弹幕
    func walkTextSpriteDescriptorWithDirection(direction : BarrageWalkDirection,
                                               textStr : String) -> BarrageDescriptor {
        
        let descriptor = BarrageDescriptor()
        descriptor.spriteName = NSStringFromClass(BarrageWalkTextSprite)
        descriptor.params["text"] = textStr
        descriptor.params["textColor"] = UIColor.whiteColor()
        descriptor.params["speed"] = "\(100 * Double(random()/Int(RAND_MAX)) + 50)"
        descriptor.params["direction"] = NSInteger(direction.rawValue)
        return descriptor
    }
    
    
    
}
